package com.paygate.dao

import com.paygate.entity.Trade
import com.paygate.long
import com.paygate.trade
import org.ktorm.database.Database
import org.ktorm.dsl.*
import org.ktorm.entity.add
import org.ktorm.entity.find
import org.ktorm.entity.update
import org.springframework.stereotype.Repository
import javax.annotation.Resource

interface TradeDao {
    fun saveTrade(trade: Trade): Int
    fun getTrade(id: long): Trade?
    fun updateTrade(trade: Trade)
}

@Repository
open class TradeDaoImpl : TradeDao {

    @Resource
    lateinit var database: Database

    override fun saveTrade(trade: Trade): Int {
        return database.trade.add(trade)
//        return database.insert(Trade.table) {
//            set(Trade.table.id, trade.id)
//            set(Trade.table.amount, trade.amount)
//            set(Trade.table.currency, trade.currency)
//            set(Trade.table.status, trade.status)
//            set(Trade.table.createDate, trade.createDate)
//            set(Trade.table.cardNo, trade.cardNo)
//            set(Trade.table.cardCvv, trade.cardCvv)
//            set(Trade.table.cardExpiredYear, trade.cardExpiredYear)
//            set(Trade.table.cardExpiredMonth, trade.cardExpiredMonth)
//            set(Trade.table.cardHolderFirstName, trade.cardHolderFirstName)
//            set(Trade.table.cardHolderLastName, trade.cardHolderLastName)
//            set(Trade.table.cardHolderEmail, trade.cardHolderEmail)
//            set(Trade.table.channelStatus, trade.channelStatus)
//            set(Trade.table.channelMsg, trade.channelMsg)
//        }
    }

   override fun getTrade(id: long): Trade? {
        return database.trade.find { it.id eq id }
    }

    override fun updateTrade(trade:Trade) {
        database.trade.update(trade)
    }
}